package com.ujs.edu.medicine.dao;

import com.ujs.edu.medicine.dataobject.YunjiayiPost;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
@Mapper
public interface YunjiayiPostMapper {
    @Delete({
        "delete from yunjiayi_post",
        "where post_id = #{postId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer postId);

    @Insert({
        "insert into yunjiayi_post (post_id, userId, ",
        "content, likeId, ",
        "commitId, create_time, ",
        "modify_time)",
        "values (#{postId,jdbcType=INTEGER}, #{userid,jdbcType=INTEGER}, ",
        "#{content,jdbcType=VARCHAR}, #{likeid,jdbcType=INTEGER}, ",
        "#{commitid,jdbcType=INTEGER}, now(), ",
        "now())"
    })
    @Options(keyProperty = "postId",keyColumn = "post_id",useGeneratedKeys = true)
    int insert(YunjiayiPost record);

    @Select({
        "select",
        "post_id, userId, content, likeId, commitId, create_time, modify_time",
        "from yunjiayi_post",
        "where post_id = #{postId,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="post_id", property="postId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="userId", property="userid", jdbcType=JdbcType.INTEGER),
        @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
        @Result(column="likeId", property="likeid", jdbcType=JdbcType.INTEGER),
        @Result(column="commitId", property="commitid", jdbcType=JdbcType.INTEGER),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP)
    })
    YunjiayiPost selectByPrimaryKey(Integer postId);

    @Select({
        "select",
        "post_id, userId, content, likeId, commitId, create_time, modify_time",
        "from yunjiayi_post"
    })
    @Results(id = "postMap",value = {
        @Result(column="post_id", property="postId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="userId", property="userid", jdbcType=JdbcType.INTEGER),
        @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
        @Result(column="likeId", property="likeid", jdbcType=JdbcType.INTEGER),
        @Result(column="commitId", property="commitid", jdbcType=JdbcType.INTEGER),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<YunjiayiPost> selectAll();

    /*模糊搜索*/
    @Select("select * from yunjiayi_post where content like '%${content}%'")
    @ResultMap("postMap")
    List<YunjiayiPost> searchLikePost(@Param("content") String content);

    /*通过用户Id查询*/
    @Select("select * from yunjiayi_post where userId=#{userId}")
    @ResultMap("postMap")
    List<YunjiayiPost>getPostByuId(@Param("userId")Integer userId);

    /*分页查询*/
    @Select("select * from yunjiayi_post limit #{startIndex}, #{pageSize}")
    @ResultMap("postMap")
    List<YunjiayiPost> selectLimit(@Param("startIndex")Integer startIndex,@Param("pageSize")Integer pageSize);


    /*查询帖子数量*/
    @Select("select count(*) from yunjiayi_post where userId=#{uId}")
    Integer selectPostNum(@Param("uId")Integer uId);


    /*通过PostID，userID查询*/
    @Select("select * from yunjiayi_post where userId=#{userId} and post_id=#{postId}")
    YunjiayiPost selectBypostAndUs(@Param("userId")Integer userId,@Param("postId")Integer postId);

    @Update({
        "update yunjiayi_post",
        "set userId = #{userid,jdbcType=INTEGER},",
          "content = #{content,jdbcType=VARCHAR},",
          "likeId = #{likeid,jdbcType=INTEGER},",
          "commitId = #{commitid,jdbcType=INTEGER},",
          "create_time = now(),",
          "modify_time = now()",
        "where post_id = #{postId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(YunjiayiPost record);
}